﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipCo.Data;
using VipCo.Entities;

public partial class Admin_DanhMuc_NhomSubTintuc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {            
            BindCatalog.BindTinTuc(cboTinTuc);
        }
    }        

    protected void Bind_Data(int idNhomTinTuc)
    {
        int TotalRows = 0;
        string whereClause = "1=1";
        if (idNhomTinTuc != 0)
        {
            whereClause = "IdNhomTinTuc = " + idNhomTinTuc;            
        }        
        TList<SubTinTuc> tlistSubTintuc = DataRepository.SubTinTucProvider.GetPaged(whereClause, "Id DESC", 0,
                                                                                    0, out TotalRows);
        gridCatalog.DataSource = tlistSubTintuc;
        gridCatalog.DataBind();
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            SubTinTuc entSubtintuc = new SubTinTuc();

            entSubtintuc.Id = Convert.ToInt32(H_CatalogId.Value);
            entSubtintuc.IdNhomTinTuc = Convert.ToInt32(cboTinTuc.SelectedValue);
            entSubtintuc.TenSubTinTuc = txtTen.Text;
            entSubtintuc.TrangThai = chkSuDung.Checked;            

            if (entSubtintuc.Id == 0)
            {
                entSubtintuc.Id = 0;
                updateStatus = DataRepository.SubTinTucProvider.Insert(entSubtintuc);
            }
            else
            {
                updateStatus = DataRepository.SubTinTucProvider.Update(entSubtintuc);
            }
            if (updateStatus)
            {
                Bind_Data(Convert.ToInt32(cboTinTuc.SelectedValue));
                cmdReset_Click(sender, e);
            }
            else
                WebMessage.Show("Cập nhật Nhóm sub tin tức thất bại!");
        }
        catch (Exception ex)
        {
            Common.WriteLog("NhomSubTinTuc.aspx.cs", "cmdSave_Click", ex.ToString());
            WebMessage.Show("Cập nhật Nhóm sub tin tức thất bại!");
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtTen.Text = "";
        chkSuDung.Checked = true;
        H_CatalogId.Value = "0";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");

            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                SubTinTuc entSubtintuc = DataRepository.SubTinTucProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entSubtintuc != null)
                {
                    txtTen.Text = entSubtintuc.TenSubTinTuc;
                    chkSuDung.Checked = entSubtintuc.TrangThai;
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("NhomSubTinTuc.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
        if (e.CommandName == "Delete")
        {
            try
            {
                int idSubTinTuc = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.SubTinTucProvider.Delete(idSubTinTuc);
                if (!deleteStatus)
                    WebMessage.Show("Xóa Nhóm Sub tin tức thất bại");
            }
            catch (Exception ex)
            {
                Common.WriteLog("NhomSubTinTuc.aspx.cs", "gridCatalog_RowCommand - Delete", ex.ToString());
                WebMessage.Show("Xóa Nhóm Sub tin tức thất bại");
            }
        }
    }

    protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Bind_Data(Convert.ToInt32(cboTinTuc.SelectedValue));
        txtTen.Text = "";
        chkSuDung.Checked = false;
        H_CatalogId.Value = "0";
    }
    
    /// <summary>
    /// Xu ly su kien khi chon du lieu
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cboTinTuc_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cboTinTuc.SelectedValue != "0")
        {
            Bind_Data(Convert.ToInt32(cboTinTuc.SelectedValue));
        }
    }
    protected void cboTinTuc_TextChanged(object sender, EventArgs e)
    {
        if (!cboTinTuc.SelectedValue.Equals("0"))
        {
            Bind_Data(Convert.ToInt32(cboTinTuc.SelectedValue));
        }
    }
}